package crowMessenger;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class EditAccountFrame extends JDialog{
	private static final long serialVersionUID = -2542456538171105687L;
	private JTextField userInput = new JTextField(20);
	private JPasswordField passInput = new JPasswordField(20);
	private Object[] status = {"Available", "Busy", "Away", "Invisible","Disconnected"};
	private JComboBox<Object> statusChange = new JComboBox<Object>(status);
	private int accountIndex;

	public EditAccountFrame(int accountIndex){
		this.accountIndex = accountIndex;
		userInput.setText(Accounts.accList.get(this.accountIndex).getUser());
		passInput.setText(Accounts.accList.get(this.accountIndex).getPass());
		statusChange.setSelectedIndex(Accounts.accList.get(this.accountIndex).getStatus());
		setTitle("Edit an existing account");
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		setLocation(600, 150);
		setResizable(false);
		setModal(true);
		getContentPane().add(editAccountPanel());
		pack();
		setVisible(true);		
	}

	private JPanel editAccountPanel(){
		JPanel panel = new JPanel(new GridLayout(2, 1));
		panel.setPreferredSize(new Dimension(500, 300));
		panel.add(upPanel());
		panel.add(downPanel());
		return panel;
	}

	/* contains the username & password and status components */
	private JPanel upPanel(){
		JPanel panel = new JPanel(new GridLayout(3, 1));
		panel.add(userPanel());
		panel.add(passPanel());
		panel.add(statusPanel());
		return panel;
	}

	private JPanel userPanel(){
		JPanel panel = new JPanel();
		JLabel label = new JLabel("Username     ");
		panel.add(label);
		panel.add(userInput);
		return panel;
	}

	private JPanel passPanel(){
		JPanel panel = new JPanel();
		JLabel label = new JLabel("Password     ");
		panel.add(label);
		panel.add(passInput);
		return panel;
	}

	private JPanel statusPanel(){
		JPanel panel = new JPanel();
		statusChange.setFocusable(false);
		statusChange.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Accounts.accList.get(accountIndex).setStatus(statusChange.getSelectedIndex());
			}
		});
		panel.add(statusChange);
		return panel;
	}

	/* contains the buttons */
	private JPanel downPanel(){
		JPanel panel = new JPanel(new GridLayout(2,4));
		//start of dummy panels
		for(int i = 0; i < 6; i++){
			JPanel tempPanel = new JPanel();
			panel.add(tempPanel);
		}
		//end of dummy panels
		panel.add(cancelPanel());
		panel.add(okPanel());
		return panel;
	}

	/* contains the ok button */
	private JPanel okPanel(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("OK");
		btn.setPreferredSize(new Dimension(100,30));
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(userInput.getText().length() >= 16 && userInput.getText().endsWith("@gmail.com") && passInput.getPassword().length >= 8){
					Accounts.accList.get(accountIndex).setUser(userInput.getText());
					Accounts.accList.get(accountIndex).setPass(new String(passInput.getPassword()));
					repaint();
					revalidate();
					setVisible(false);
				}
			}
		});
		panel.add(btn);
		return panel;
	}

	/* contains the cancel button */
	private JPanel cancelPanel(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("Cancel");
		btn.setPreferredSize(new Dimension(100,30));
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		panel.add(btn);
		return panel;
	}
}
